package api

import (
	"getaway/basic/config"
	__ "getaway/basic/proto/userproto"
	"getaway/handler/request"
	"getaway/handler/response"
	"getaway/middleware"
	"github.com/dgrijalva/jwt-go"
	"github.com/gin-gonic/gin"
	"time"
)

// Register 注册用户
// @Summary 注册用户
// @Description 注册用户
// @Tags 用户
// @Accept json
// @Produce json
// @Param req body request.Register true "注册用户请求参数"
// @Success 200 {object} response.Response  "注册用户成功"
// @Failure 400 {object} response.Response "请求参数错误"
// @Failure 500 {object} response.Response "服务连接失败"
// @Router /user/register [post]
func Register(c *gin.Context) {
	var req request.Register
	if err := c.ShouldBind(&req); err != nil {
		response.SrvError(c, err.Error(), nil)
		return
	}

	user, err := config.UserClient.RegisterUser(c, &__.RegisterUserReq{
		Mobile:   req.Mobile,
		Password: req.Password,
	})
	if err != nil {
		response.SrvError(c, err.Error(), "服务连接失败")
		return
	}

	// 记录注册行为
	userId, _ := c.Get("userId")
	if userId == nil && user != nil {
		userId = user.Id
	}
	middleware.RecordClickBehavior(c, userId, "用户注册", map[string]interface{}{
		"mobile":   req.Mobile,
		"nickName": req.NickName,
		"success":  err == nil,
	})

	response.Success(c, "200", user)
}

// Login
// @Summary 登录
// @Description 登录
// @Tags 用户
// @Accept json
// @Produce json
// @Param req body request.Login true "登录请求参数"
// @Success 200 {object} response.Response "登录成功"
// @Failure 400 {object} response.Response "请求参数错误"
// @Failure 500 {object} response.Response "服务连接失败"
// @Router /user/login [post]
func Login(c *gin.Context) {
	var req request.Login
	if err := c.ShouldBind(&req); err != nil {
		response.SrvError(c, err.Error(), nil)
		return
	}
	resp, err := config.UserClient.LoginUser(c, &__.LoginUserReq{
		Mobile:   req.Mobile,
		Password: req.Password,
	})
	if err != nil {
		response.SrvError(c, err.Error(), "服务连接失败")
		return
	}
	token, err := middleware.NewJWT(config.KEY_JWT).CreateToken(middleware.CustomClaims{
		ID:          uint(resp.Id),
		AuthorityId: uint(resp.UserRole),
		StandardClaims: jwt.StandardClaims{
			ExpiresAt: time.Now().Unix() + 86400*7,
		},
	})
	if err != nil {
		return
	}

	// 记录登录行为
	userId, _ := c.Get("userId")
	if userId == nil {
		userId = resp.Id
	}
	middleware.RecordClickBehavior(c, userId, "用户登录", map[string]interface{}{
		"mobile":  req.Mobile,
		"success": err == nil,
	})

	response.Success(c, "200", token)
}

// UserWallet
// @Summary 用户钱包
// @Description 用户钱包
// @Tags 用户
// @Accept json
// @Produce json
// @Param req body request.UserWallet true "用户钱包请求参数"
// @Success 200 {object} response.Response"用户钱包成功"
// @Failure 400 {object} response.Response "请求参数错误"
// @Failure 500 {object} response.Response "服务连接失败"
// @Router /user/wallet [post]
func UserWallet(c *gin.Context) {
	var req request.UserWallet
	if err := c.ShouldBind(&req); err != nil {
		response.SrvError(c, err.Error(), nil)
		return
	}
	resp, err := config.UserClient.UserWallet(c, &__.UserWalletReq{
		UserId:                   int64(req.UserId),
		UserAvailableBalance:     req.UserAvailableBalance,
		UserFrozenBalance:        req.UserFrozenBalance,
		UserInvoiceableAmount:    req.UserInvoiceableAmount,
		UninvoicedAmountForTrips: req.UninvoicedAmountForTrips,
	})
	if err != nil {
		response.SrvError(c, err.Error(), "服务连接失败")
		return
	}

	// 记录用户钱包操作行为
	userId, _ := c.Get("userId")
	if userId == nil {
		userId = req.UserId
	}
	middleware.RecordClickBehavior(c, userId, "用户钱包操作", map[string]interface{}{
		"userId":           req.UserId,
		"availableBalance": req.UserAvailableBalance,
		"success":          err == nil,
	})

	response.Success(c, "200", resp)
}

// IndividualUser
// @Summary 用户个人信息
// @Description 用户个人信息
// @Tags 用户
// @Accept json
// @Produce json
// @Param req body request.IndividualUserReq true "用户个人信息请求参数"
// @Success 200 {object} response.Response"用户个人信息成功"
// @Failure 400 {object} response.Response "请求参数错误"
// @Failure 500 {object} response.Response "服务连接失败"
// @Router /user/individualUser [post]
func IndividualUser(c *gin.Context) {
	var req request.IndividualUserReq
	if err := c.ShouldBind(&req); err != nil {
		response.SrvError(c, err.Error(), nil)
		return
	}
	resp, err := config.UserClient.IndividualUser(c, &__.IndividualUserReq{
		Name:                req.Name,
		Mobile:              req.Mobile,
		EnterpriseUser:      req.EnterpriseUser,
		MemberLevel:         req.MemberLevel,
		UserPrice:           float32(req.UserPrice),
		RegistrationChannel: req.RegistrationChannel,
	})
	if err != nil {
		response.SrvError(c, err.Error(), "服务连接失败")
		return
	}

	// 记录用户个人信息查询行为
	userId, _ := c.Get("userId")
	middleware.RecordClickBehavior(c, userId, "用户个人信息查询", map[string]interface{}{
		"mobile":         req.Mobile,
		"enterpriseUser": req.EnterpriseUser,
		"success":        err == nil,
	})

	response.Success(c, "200", resp)
}

// UserConsumptionInformation
// @Summary 用户消费记录
// @Description 用户消费记录
// @Tags 用户
// @Accept json
// @Produce json
// @Param req body request.UserConsumptionInformationReq true "用户消费记录请求参数"
// @Success 200 {object} response.Response "用户消费记录成功"
// @Failure 400 {object} response.Response "请求参数错误"
// @Failure 500 {object} response.Response "服务连接失败"
// @Router /user/userConsumptionInformation [post]
func UserConsumptionInformation(c *gin.Context) {
	var req request.UserConsumptionInformationReq
	if err := c.ShouldBind(&req); err != nil {
		response.SrvError(c, err.Error(), nil)
		return
	}
	resp, err := config.UserClient.UserConsumptionInformation(c, &__.UserConsumptionInformationReq{
		UserId:                    req.UserId,
		PayNo:                     req.PayNo,
		Event:                     req.Event,
		PriceTime:                 req.PriceTime,
		OrderNo:                   req.OrderNo,
		GiftAmount:                req.GiftAmount,
		RechargeAmountPrincipal:   req.RechargeAmountPrincipal,
		TotalRechargeAmount:       req.TotalRechargeAmount,
		RefundAmount:              req.RefundAmount,
		TotalOrderPrice:           float32(req.TotalOrderPrice),
		MonthlyOrderPaymentAmount: req.MonthlyOrderPaymentAmount,
	})
	if err != nil {
		response.SrvError(c, err.Error(), "服务连接失败")
		return
	}

	// 记录用户消费记录查询行为
	userId, _ := c.Get("userId")
	if userId == nil {
		userId = req.UserId
	}
	middleware.RecordClickBehavior(c, userId, "用户消费记录查询", map[string]interface{}{
		"userId":  req.UserId,
		"orderNo": req.OrderNo,
		"success": err == nil,
	})

	response.Success(c, "200", resp)
}

// UserInvoice
// @Summary 用户发票
// @Description 用户发票
// @Tags 用户
// @Accept json
// @Produce json
// @Param req body request.UserInvoiceReq true "用户发票请求参数"
// @Success 200 {object} response.Response "用户发票成功"
// @Failure 400 {object} response.Response "请求参数错误"
// @Failure 500 {object} response.Response "服务连接失败"
// @Router /user/userInvoice [post]
func UserInvoice(c *gin.Context) {
	var req request.UserInvoiceReq
	if err := c.ShouldBind(&req); err != nil {
		response.SrvError(c, err.Error(), nil)
		return
	}
	resp, err := config.UserClient.UserInvoice(c, &__.UserInvoiceReq{
		Name:                    req.Name,
		TaxIdentificationNumber: req.TaxIdentificationNumber,
		Address:                 req.Address,
		Mobile:                  req.Mobile,
		BankOfDeposit:           req.BankOfDeposit,
		BankAccount:             req.BankAccount,
	})
	if err != nil {
		response.SrvError(c, err.Error(), "服务连接失败")
		return
	}

	// 记录用户发票操作行为
	userId, _ := c.Get("userId")
	middleware.RecordClickBehavior(c, userId, "用户发票操作", map[string]interface{}{
		"name":    req.Name,
		"mobile":  req.Mobile,
		"success": err == nil,
	})

	response.Success(c, "200", resp)
}

// UserInvoiceDetail
// @Summary 用户发票详情
// @Description 用户发票详情
// @Tags 用户
// @Accept json
// @Produce json
// @Param req body request.UserInvoiceDetailReq true "用户发票详情请求参数"
// @Success 200 {object} response.Response{data=__.UserInvoiceDetailResp} "用户发票详情成功"
// @Failure 400 {object} response.Response "请求参数错误"
// @Failure 500 {object} response.Response "服务连接失败"
// @Router /user/userinvoicedetail [get]
func UserInvoiceDetail(c *gin.Context) {
	var req request.UserInvoiceDetailReq
	if err := c.ShouldBind(&req); err != nil {
		response.SrvError(c, err.Error(), nil)
		return
	}
	resp, err := config.UserClient.UserInvoiceDetail(c, &__.UserInvoiceDetailReq{
		Id: req.Id,
	})
	if err != nil {
		response.SrvError(c, err.Error(), "服务连接失败")
		return
	}

	// 记录用户发票详情查询行为
	userId, _ := c.Get("userId")
	middleware.RecordClickBehavior(c, userId, "用户发票详情查询", map[string]interface{}{
		"invoiceId": req.Id,
		"success":   err == nil,
	})

	response.Success(c, "200", resp)
}

// UserindelDetail
// @Summary 用户indel详情
func UserindelDetail(c *gin.Context) {
	var req request.UserindelDetailReq
	if err := c.ShouldBind(&req); err != nil {
		response.SrvError(c, err.Error(), nil)
		return
	}
	resp, err := config.UserClient.UserindelDetail(c, &__.UserindelDetailReq{
		Id: req.Id,
	})
	if err != nil {
		response.SrvError(c, err.Error(), "服务连接失败")
		return
	}

	// 记录用户indel详情查询行为
	userId, _ := c.Get("userId")
	middleware.RecordClickBehavior(c, userId, "用户indel详情查询", map[string]interface{}{
		"id":      req.Id,
		"success": err == nil,
	})

	response.Success(c, "200", resp)
}

// UserConsumptionInformationDetail
// @Summary 用户消费详情
// @Description 用户消费详情
// @Tags 用户
// @Accept json
// @Produce json
// @Param req body request.UserConsumptionInformationDetailReq true "用户消费详情请求参数"
// @Success 200 {object} response.Response"用户消费详情成功"
// @Failure 400 {object} response.Response "请求参数错误"
// @Failure 500 {object} response.Response "服务连接失败"
// @Router /user/userConsumptionInformationDetail [post]
func UserConsumptionInformationDetail(c *gin.Context) {
	var req request.UserConsumptionInformationDetailReq
	if err := c.ShouldBind(&req); err != nil {
		response.SrvError(c, err.Error(), nil)
		return
	}
	resp, err := config.UserClient.UserConsumptionInformationDetail(c, &__.UserConsumptionInformationDetailReq{
		UserId: req.UserId,
	})
	if err != nil {
		response.SrvError(c, err.Error(), "服务连接失败")
		return
	}

	// 记录用户消费详情查询行为
	userId, _ := c.Get("userId")
	if userId == nil {
		userId = req.UserId
	}
	middleware.RecordClickBehavior(c, userId, "用户消费详情查询", map[string]interface{}{
		"userId":  req.UserId,
		"success": err == nil,
	})

	response.Success(c, "200", resp)
}

// RealName
// @Summary 用户实名认证
// @Description 用户实名认证
// @Tags 用户
// @Accept json
// @Produce json
// @Param req body request.RealNameReq true "用户实名认证请求参数"
// @Success 200 {object} response.Response"用户实名认证成功"
// @Failure 400 {object} response.Response "请求参数错误"
// @Failure 500 {object} response.Response "服务连接失败"
// @Router /user/realName [post]
func RealName(c *gin.Context) {
	var req request.RealNameReq
	if err := c.ShouldBind(&req); err != nil {
		response.SrvError(c, err.Error(), nil)
		return
	}
	resp, err := config.UserClient.RealName(c, &__.RealNameReq{
		UserId:   int64(req.UserId),
		RealName: req.RealName,
		IdCard:   req.IdCard,
	})
	if err != nil {
		response.SrvError(c, err.Error(), "服务连接失败")
		return
	}

	// 记录实名认证行为
	userId, _ := c.Get("userId")
	if userId == nil {
		userId = req.UserId
	}
	middleware.RecordClickBehavior(c, userId, "用户实名认证", map[string]interface{}{
		"userId":   req.UserId,
		"realName": req.RealName,
		"success":  err == nil,
	})

	response.Success(c, "200", resp)
}

// ListTerminal
// @Summary 用户终端列表
// @Description 用户终端列表
// @Tags 用户
// @Accept json
// @Produce json
// @Param req body request.ListTerminalReq true "用户终端列表请求参数"
// @Success 200 {object} response.Response "用户终端列表成功"
// @Failure 400 {object} response.Response "请求参数错误"
// @Failure 500 {object} response.Response "服务连接失败"
// @Router /user/listTerminal [post]
func ListTerminal(c *gin.Context) {
	var req request.ListTerminalReq
	if err := c.ShouldBind(&req); err != nil {
		response.SrvError(c, err.Error(), nil)
		return
	}
	resp, err := config.UserClient.ListTerminal(c, &__.ListTerminalReq{
		Terminal: req.Terminal,
	})
	if err != nil {
		response.SrvError(c, err.Error(), "服务连接失败")
		return
	}
	response.Success(c, "ok", resp)
}

// ListMobile
// @Summary 用户手机号列表
// @Description 用户手机号列表
// @Tags 用户
// @Accept json
// @Produce json
// @Param req body request.ListMobileReq true "用户手机号列表请求参数"
// @Success 200 {object} response.Response"用户手机号列表成功"
// @Failure 400 {object} response.Response "请求参数错误"
// @Failure 500 {object} response.Response "服务连接失败"
// @Router /user/listMobile [post]
func ListMobile(c *gin.Context) {
	var req request.ListMobileReq
	if err := c.ShouldBind(&req); err != nil {
		response.SrvError(c, err.Error(), nil)
		return
	}
	resp, err := config.UserClient.ListMobile(c, &__.ListMobileReq{
		Mobile: req.Mobile,
	})
	if err != nil {
		response.SrvError(c, err.Error(), "服务连接失败")
		return
	}
	response.Success(c, "ok", resp)
}

// List
// @Summary 用户列表
// @Description 用户列表
// @Tags 用户
// @Accept json
// @Produce json
// @Param req body request.ListReq true "用户列表请求参数"
// @Success 200 {object} response.Response "用户列表成功"
// @Failure 400 {object} response.Response "请求参数错误"
// @Failure 500 {object} response.Response "服务连接失败"
// @Router /user/list [post]
func List(c *gin.Context) {
	var req request.ListReq
	if err := c.ShouldBind(&req); err != nil {
		response.SrvError(c, err.Error(), nil)
		return
	}
	resp, err := config.UserClient.List(c, &__.ListReq{})
	if err != nil {
		response.SrvError(c, err.Error(), "服务连接失败")
		return
	}
	response.Success(c, "ok", resp)
}

// ListPhoneStatus
// @Summary 用户手机号状态列表
// @Description 用户手机号状态列表
// @Tags 用户
// @Accept json
// @Produce json
// @Param req body request.ListPhoneStatusReq true "用户手机号状态列表请求参数"
// @Success 200 {object} response.Response "用户手机号状态列表成功"
// @Failure 400 {object} response.Response "请求参数错误"
// @Failure 500 {object} response.Response "服务连接失败"
// @Router /user/listPhoneStatus [post]
func ListPhoneStatus(c *gin.Context) {
	var req request.ListPhoneStatusReq
	if err := c.ShouldBind(&req); err != nil {
		response.SrvError(c, err.Error(), nil)
		return
	}
	resp, err := config.UserClient.ListPhoneStatus(c, &__.ListPhoneStatusReq{
		PhoneStatus: int64(req.PhoneStatus),
	})
	if err != nil {
		response.SrvError(c, err.Error(), "服务连接失败")
		return
	}
	response.Success(c, "ok", resp)
}

func ListTaxIdentificationNumber(c *gin.Context) {
	var req request.ListTaxIdentificationNumberReq
	if err := c.ShouldBind(&req); err != nil {
		response.SrvError(c, err.Error(), nil)
		return
	}
	resp, err := config.UserClient.ListTaxIdentificationNumber(c, &__.ListTaxIdentificationNumberReq{
		TaxIdentificationNumber: req.TaxIdentificationNumber,
	})
	if err != nil {
		response.SrvError(c, err.Error(), "服务连接失败")
		return
	}
	response.Success(c, "ok", resp)
}

// UpdateUser
// @Summary 修改用户信息
// @Description 修改用户信息
// @Tags 用户
// @Accept json
// @Produce json
// @Param req body request.UpdateUserReq true "修改用户信息请求参数"
// @Success 200 {object} response.Response"修改用户信息成功"
// @Failure 400 {object} response.Response "请求参数错误"
// @Failure 500 {object} response.Response "服务连接失败"
// @Router /user/updateuser [post]
func UpdateUser(c *gin.Context) {
	var req request.UpdateUserReq
	if err := c.ShouldBind(&req); err != nil {
		response.SrvError(c, err.Error(), nil)
		return
	}
	resp, err := config.UserClient.UpdateUser(c, &__.UpdateUserReq{
		UserId:                  req.UserId,
		NickName:                req.NickName,
		RealName:                req.RealName,
		IdCard:                  req.IdCard,
		Sex:                     req.Sex,
		Address:                 req.Address,
		UserRegisterTime:        req.UserRegisterTime,
		UserLoginTime:           req.UserLoginTime,
		LastLoginMethod:         uint32(req.LastLoginMethod),
		LastLoginTimeOnScreen:   req.LastLoginTimeOnScreen,
		LastLoginMethodOnScreen: uint32(req.LastLoginMethodOnScreen),
		LastOrderTime:           req.LastOrderTime,
		CType:                   req.CType,
		UserRole:                int64(req.UserRole),
	})
	if err != nil {
		response.SrvError(c, err.Error(), "服务连接失败")
		return
	}
	response.Success(c, "ok", resp)
}

// SendSms
// @Summary 发送短信
// @Description 发送短信
// @Tags 用户
// @Accept json
// @Produce json
// @Param req body request.SendSmsReq true "发送短信请求参数"
// @Success 200 {object} response.Response "发送短信成功"
// @Failure 400 {object} response.Response "请求参数错误"
// @Failure 500 {object} response.Response "服务连接失败"
// @Router /user/send [post]
func SendSms(c *gin.Context) {
	var req request.SendSmsReq
	if err := c.ShouldBind(&req); err != nil {
		response.SrvError(c, err.Error(), nil)
		return
	}
	resp, err := config.UserClient.SendSms(c, &__.SendSmsReq{
		Mobile: req.Mobile,
	})
	if err != nil {
		response.SrvError(c, err.Error(), "服务连接失败")
		return
	}
	response.Success(c, "发送成功", resp)
}

// PersonalUserRecharge
// @Summary 个人用户充值记录
// @Description 个人用户充值记录
// @Tags 用户
// @Accept json
// @Produce json
// @Param req body request.PersonalUserRechargeReq true "个人用户充值记录请求参数"
// @Success 200 {object} response.Response{data=__.PersonalUserRechargeResp} "个人用户充值记录成功"
// @Failure 400 {object} response.Response "请求参数错误"
// @Failure 500 {object} response.Response "服务连接失败"
// @Router /user/personalUserRecharge [post]
func PersonalUserRecharge(c *gin.Context) {
	var req request.PersonalUserRechargeReq
	if err := c.ShouldBind(&req); err != nil {
		response.SrvError(c, err.Error(), nil)
		return
	}
	resp, err := config.UserClient.PersonalUserRecharge(c, &__.PersonalUserRechargeReq{
		UserId:                  req.UserId,
		RechargeTransactionId:   req.RechargeTransactionId,
		UserName:                req.UserName,
		Mobile:                  req.Mobile,
		UserType:                req.UserType,
		RechargeTime:            req.RechargeTime,
		RechargeAmountPrincipal: req.RechargeAmountPrincipal,
		GiftAmount:              req.GiftAmount,
		RechargeDiscount:        req.RechargeDiscount,
		ThirdPartyTransactionId: req.ThirdPartyTransactionId,
		RechargeChannel:         req.RechargeChannel,
	})
	if err != nil {
		response.SrvError(c, err.Error(), "服务连接失败")
		return
	}

	// 记录个人用户充值记录操作行为
	userId, _ := c.Get("userId")
	if userId == nil {
		userId = req.UserId
	}
	middleware.RecordClickBehavior(c, userId, "个人用户充值记录", map[string]interface{}{
		"userId":                req.UserId,
		"rechargeTransactionId": req.RechargeTransactionId,
		"userName":              req.UserName,
		"mobile":                req.Mobile,
		"success":               err == nil,
	})

	response.Success(c, "200", resp)
}

// PersonalUserRefund
// @Summary 个人用户退款记录
// @Description 个人用户退款记录
// @Tags 用户
// @Accept json
// @Produce json
// @Param req body request.PersonalUserRefundReq true "个人用户退款记录请求参数"
// @Success 200 {object} response.Response{data=__.PersonalUserRefundResp} "个人用户退款记录成功"
// @Failure 400 {object} response.Response "请求参数错误"
// @Failure 500 {object} response.Response "服务连接失败"
// @Router /user/personalUserRefund [post]
func PersonalUserRefund(c *gin.Context) {
	var req request.PersonalUserRefundReq
	if err := c.ShouldBind(&req); err != nil {
		response.SrvError(c, err.Error(), nil)
		return
	}
	resp, err := config.UserClient.PersonalUserRefund(c, &__.PersonalUserRefundReq{
		UserId:                  req.UserId,
		RefundTransactionId:     req.RefundTransactionId,
		UserName:                req.UserName,
		Mobile:                  req.Mobile,
		UserType:                req.UserType,
		RefundTime:              req.RefundTime,
		RefundAmount:            req.RefundAmount,
		RefundReason:            req.RefundReason,
		ThirdPartyTransactionId: req.ThirdPartyTransactionId,
		RefundChannel:           req.RefundChannel,
		RefundStatus:            req.RefundStatus,
	})
	if err != nil {
		response.SrvError(c, err.Error(), "服务连接失败")
		return
	}

	// 记录个人用户退款记录操作行为
	userId, _ := c.Get("userId")
	if userId == nil {
		userId = req.UserId
	}
	middleware.RecordClickBehavior(c, userId, "个人用户退款记录", map[string]interface{}{
		"userId":              req.UserId,
		"refundTransactionId": req.RefundTransactionId,
		"userName":            req.UserName,
		"mobile":              req.Mobile,
		"success":             err == nil,
	})

	response.Success(c, "200", resp)
}

// PersonalUserRechargeList
// @Summary 个人用户充值记录列表
// @Description 个人用户充值记录列表查询
// @Tags 用户
// @Accept json
// @Produce json
// @Param req query request.PersonalUserRechargeListReq true "个人用户充值记录查询参数"
// @Success 200 {object} response.Response{data=__.PersonalUserRechargeListResp} "查询成功"
// @Failure 400 {object} response.Response "请求参数错误"
// @Failure 500 {object} response.Response "服务连接失败"
// @Router /user/personalUserRechargeList [get]
func PersonalUserRechargeList(c *gin.Context) {
	var req request.PersonalUserRechargeListReq
	if err := c.ShouldBind(&req); err != nil {
		response.SrvError(c, err.Error(), nil)
		return
	}

	// 设置默认分页参数
	if req.Page <= 0 {
		req.Page = 1
	}
	if req.PageSize <= 0 {
		req.PageSize = 10
	}

	resp, err := config.UserClient.PersonalUserRechargeList(c, &__.PersonalUserRechargeListReq{
		RechargeTransactionId: req.RechargeTransactionId,
		Mobile:                req.Mobile,
		UserName:              req.UserName,
		RechargeChannel:       req.RechargeChannel,
		StartTime:             req.StartTime,
		EndTime:               req.EndTime,
		Page:                  req.Page,
		PageSize:              req.PageSize,
	})
	if err != nil {
		response.SrvError(c, err.Error(), "服务连接失败")
		return
	}

	// 记录查询行为
	userId, _ := c.Get("userId")
	middleware.RecordClickBehavior(c, userId, "个人用户充值记录查询", map[string]interface{}{
		"rechargeTransactionId": req.RechargeTransactionId,
		"mobile":                req.Mobile,
		"userName":              req.UserName,
		"rechargeChannel":       req.RechargeChannel,
		"startTime":             req.StartTime,
		"endTime":               req.EndTime,
		"page":                  req.Page,
		"pageSize":              req.PageSize,
		"success":               err == nil,
	})

	response.Success(c, "查询成功", resp)
}

// PersonalUserRefundList
// @Summary 个人用户退款记录列表
// @Description 个人用户退款记录列表查询
// @Tags 用户
// @Accept json
// @Produce json
// @Param req query request.PersonalUserRefundListReq true "个人用户退款记录查询参数"
// @Success 200 {object} response.Response{data=__.PersonalUserRefundListResp} "查询成功"
// @Failure 400 {object} response.Response "请求参数错误"
// @Failure 500 {object} response.Response "服务连接失败"
// @Router /user/personalUserRefundList [get]
func PersonalUserRefundList(c *gin.Context) {
	var req request.PersonalUserRefundListReq
	if err := c.ShouldBind(&req); err != nil {
		response.SrvError(c, err.Error(), nil)
		return
	}

	// 设置默认分页参数
	if req.Page <= 0 {
		req.Page = 1
	}
	if req.PageSize <= 0 {
		req.PageSize = 10
	}

	resp, err := config.UserClient.PersonalUserRefundList(c, &__.PersonalUserRefundListReq{
		RefundTransactionId: req.RefundTransactionId,
		Mobile:              req.Mobile,
		UserName:            req.UserName,
		RefundChannel:       req.RefundChannel,
		StartTime:           req.StartTime,
		EndTime:             req.EndTime,
		Page:                req.Page,
		PageSize:            req.PageSize,
	})
	if err != nil {
		response.SrvError(c, err.Error(), "服务连接失败")
		return
	}

	// 记录查询行为
	userId, _ := c.Get("userId")
	middleware.RecordClickBehavior(c, userId, "个人用户退款记录查询", map[string]interface{}{
		"refundTransactionId": req.RefundTransactionId,
		"mobile":              req.Mobile,
		"userName":            req.UserName,
		"refundChannel":       req.RefundChannel,
		"startTime":           req.StartTime,
		"endTime":             req.EndTime,
		"page":                req.Page,
		"pageSize":            req.PageSize,
		"success":             err == nil,
	})

	response.Success(c, "查询成功", resp)
}

// ExportPersonalUserRecharge
// @Summary 导出个人用户充值记录
// @Description 导出个人用户充值记录
// @Tags 用户
// @Accept json
// @Produce json
// @Param req body request.ExportPersonalUserRechargeReq true "导出个人用户充值记录参数"
// @Success 200 {object} response.Response{data=__.ExportDataResp} "导出成功"
// @Failure 400 {object} response.Response "请求参数错误"
// @Failure 500 {object} response.Response "服务连接失败"
// @Router /user/exportPersonalUserRecharge [post]
func ExportPersonalUserRecharge(c *gin.Context) {
	var req request.ExportPersonalUserRechargeReq
	if err := c.ShouldBind(&req); err != nil {
		response.SrvError(c, err.Error(), nil)
		return
	}

	resp, err := config.UserClient.ExportPersonalUserRecharge(c, &__.ExportPersonalUserRechargeReq{
		RechargeTransactionId: req.RechargeTransactionId,
		Mobile:                req.Mobile,
		UserName:              req.UserName,
		RechargeChannel:       req.RechargeChannel,
		StartTime:             req.StartTime,
		EndTime:               req.EndTime,
		ExportType:            req.ExportType,
	})
	if err != nil {
		response.SrvError(c, err.Error(), "服务连接失败")
		return
	}

	// 记录导出行为
	userId, _ := c.Get("userId")
	middleware.RecordClickBehavior(c, userId, "导出个人用户充值记录", map[string]interface{}{
		"rechargeTransactionId": req.RechargeTransactionId,
		"mobile":                req.Mobile,
		"userName":              req.UserName,
		"rechargeChannel":       req.RechargeChannel,
		"startTime":             req.StartTime,
		"endTime":               req.EndTime,
		"exportType":            req.ExportType,
		"success":               err == nil,
	})

	response.Success(c, "导出成功", resp)
}

// ExportPersonalUserRefund
// @Summary 导出个人用户退款记录
// @Description 导出个人用户退款记录
// @Tags 用户
// @Accept json
// @Produce json
// @Param req body request.ExportPersonalUserRefundReq true "导出个人用户退款记录参数"
// @Success 200 {object} response.Response{data=__.ExportDataResp} "导出成功"
// @Failure 400 {object} response.Response "请求参数错误"
// @Failure 500 {object} response.Response "服务连接失败"
// @Router /user/exportPersonalUserRefund [post]
func ExportPersonalUserRefund(c *gin.Context) {
	var req request.ExportPersonalUserRefundReq
	if err := c.ShouldBind(&req); err != nil {
		response.SrvError(c, err.Error(), nil)
		return
	}

	resp, err := config.UserClient.ExportPersonalUserRefund(c, &__.ExportPersonalUserRefundReq{
		RefundTransactionId: req.RefundTransactionId,
		Mobile:              req.Mobile,
		UserName:            req.UserName,
		RefundChannel:       req.RefundChannel,
		StartTime:           req.StartTime,
		EndTime:             req.EndTime,
		ExportType:          req.ExportType,
	})
	if err != nil {
		response.SrvError(c, err.Error(), "服务连接失败")
		return
	}

	// 记录导出行为
	userId, _ := c.Get("userId")
	middleware.RecordClickBehavior(c, userId, "导出个人用户退款记录", map[string]interface{}{
		"refundTransactionId": req.RefundTransactionId,
		"mobile":              req.Mobile,
		"userName":            req.UserName,
		"refundChannel":       req.RefundChannel,
		"startTime":           req.StartTime,
		"endTime":             req.EndTime,
		"exportType":          req.ExportType,
		"success":             err == nil,
	})

	response.Success(c, "导出成功", resp)
}
